package com.yanqu.road.server.gameplayer.module.manor.fishPondTaskCondition;

import com.yanqu.road.entity.config.fishpond.FishPondEventInfo;
import com.yanqu.road.entity.enums.eFishPondEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.event.EventTriggerArgs;
import com.yanqu.road.entity.event.IEventListener;
import com.yanqu.road.entity.log.LogFishPondItem;
import com.yanqu.road.entity.manor.FishPondSpecialItemData;
import com.yanqu.road.entity.manor.FishPondTaskData;
import com.yanqu.road.entity.task.TaskConditionInfo;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.manor.ManorFishPondModel;
import com.yanqu.road.server.gameplayer.module.manor.ManorModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.manger.config.TaskMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.manor.FishPondMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.Date;

/**
 * @author ：zfr
 * @date ：2021/10/11 17:12
 */
public abstract class BaseFishPondCondition {
    protected GamePlayer player;
    private FishPondTaskData fishPondTaskData;
    private Object initArgs;
    private int eventType;

    public BaseFishPondCondition(GamePlayer player, FishPondTaskData fishPondTaskData, Object initArgs, int eventType) {
        this.player = player;
        this.fishPondTaskData = fishPondTaskData;
        this.initArgs = initArgs;
        this.eventType = eventType;
    }

    public void setValue(BigInteger value) {
        fishPondTaskData.setValue(value);
        ManorFishPondModel model = player.getModule(ManorModule.class).getManorFishPondModel();
        if (model != null && model.isHundredFishing() && !model.isRedWorm() && model.getFishPondData().isAutoFinishTask() && isCompleted()) {
            model.getTaskReward(fishPondTaskData.getId());
        }
    }

    public BigInteger getValue() {
        return fishPondTaskData.getValue();
    }

    public FishPondTaskData getFishPondTaskData() {
        return fishPondTaskData;
    }

    public Object getInitArgs() {
        return initArgs;
    }

    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eventType, handle);
    }

    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eventType, handle);
    }


    public  boolean isCompleted(){
        TaskConditionInfo taskConditionInfo = TaskMgr.getTaskConditionById(fishPondTaskData.getTaskId());
        return getValue().compareTo(taskConditionInfo.getParamList().get(3)) >= 0;
    }

    public boolean isGetReward() {
        return fishPondTaskData.isGetReward();
    }

    public void initFishPondTaskAction() {
        addToPlayer();
        initCondition();
        initValue();
    }

    public void addToPlayer() {
        if (!isCompleted() || isSpecialItemTask(eventType)) {
            addTrigger(player);
        }
    }

    public void initCondition() {

    }

    public void initValue() {

    }

    public int finish(GamePlayer player) {
        FishPondEventInfo eventInfo = FishPondMgr.getFishPondEventInfo(fishPondTaskData.getEventId());
        if (eventInfo == null) {
            return GameErrorCode.E_TASK_NO_FOUND;
        }
        if (!isCompleted()) {
            return GameErrorCode.E_TASK_NOT_COMPLETE;
        }
        if (fishPondTaskData.isGetReward()) {
            return GameErrorCode.E_TASK_ALREADY_GET_REWARD;
        }
        ManorFishPondModel fishPondModel = player.getModule(ManorModule.class).getManorFishPondModel();
        if (isSpecialItemTask(eventType)) {
            // 交付
            TaskConditionInfo taskConditionInfo = TaskMgr.getTaskConditionById(fishPondTaskData.getTaskId());
            FishPondSpecialItemData data = fishPondModel.getFishPondSpecialItemDataMap().get(taskConditionInfo.getParamList().get(2).intValue());
            long needCount = taskConditionInfo.getParamList().get(3).longValue();
            if (data == null || data.getCount() < needCount) {
                return GameErrorCode.E_TASK_NOT_COMPLETE;
            }
            data.setCount(data.getCount() - needCount);
            fishPondModel.syncSingleSpecialItem(data);

            fishPondModel.notifyFishingSpecialItemTask(data);
            //日志
            AutoLogMgr.add(new LogFishPondItem(player.getUserId(), data.getSpecialItemId(), false, needCount, data.getCount(), 0, eLogMoneyType.ManorFishPondTaskCost.getValue()));
        }
        if (fishPondModel.isHundredFishing()) {
            fishPondModel.getHundredFishingFinishTaskReward().addProperty(PropertyHelper.parseStringToProperty(eventInfo.getReward()));
            fishPondModel.getHundredFishingFinishTaskList().add(fishPondTaskData.getTaskId());
        }
        //领奖
        fishPondTaskData.setGetReward(true);
        removeTrigger(player);
        player.getModule(CurrencyModule.class).addCurrency(eventInfo.getReward(), eLogMoneyType.Manor, eLogMoneyType.ManorFishPondGetTaskReward);

        return 0;
    }

    public static BaseFishPondCondition createCondition(GamePlayer player, FishPondTaskData fishPondTaskData) {
        TaskConditionInfo taskConditionInfo = TaskMgr.getTaskConditionById(fishPondTaskData.getTaskId());
        if (taskConditionInfo == null) {
            return null;
        }
        int type = taskConditionInfo.getType();
        if (eFishPondEventType.FishingTimes.getIntValue() == type) {
            return new FishPondFishingCondition(player, fishPondTaskData, null, type);
        } else if (eFishPondEventType.FishingFishCount.getIntValue() == type) {
            return new FishingFishCountCondition(player, fishPondTaskData, null, type);
        } else if (eFishPondEventType.FishingQualityFishCount.getIntValue() == type) {
            return new FishingQualityFishCountCondition(player, fishPondTaskData, null, type);
        } else if (eFishPondEventType.FishingTypeFishCount.getIntValue() == type) {
            return new FishingTypeFishCountCondition(player, fishPondTaskData, null, type);
        } else if (eFishPondEventType.FishingTypeSpecialItemCount.getIntValue() == type) {
            int itemId = taskConditionInfo.getParamList().get(2).intValue();
            FishPondSpecialItemData specialItemData = null;
            ManorFishPondModel model = player.getModule(ManorModule.class).getManorFishPondModel();
            if (model != null) {
                specialItemData = model.getFishPondSpecialItemDataMap().get(itemId);
            }
            return new FishingTypeSpecialItemCountCondition(player, fishPondTaskData, specialItemData, type);
        }
        return null;
    }

    public boolean isSpecialItemTask(int eventType) {
        return eventType == eFishPondEventType.FishingTypeSpecialItemCount.getIntValue();
    }

    protected abstract void changeValue(Object object);

    protected final Player_BoardLevelHandle handle = new Player_BoardLevelHandle();

    public class Player_BoardLevelHandle implements IEventListener {
        @Override
        public void triggerEvent(EventTriggerArgs eventArgs) {
            changeValue(eventArgs.getArgs());
        }
    }




}
